//
//  HNPayTypeController.m
//  baidaifu
//
//  Created by 朱传正 on 2016/12/23.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNPayTypeController.h"
#import "HNDocterMsgCell.h"
#import "HNPaySuccessController.h"
#import "HNAllJSExport.h"
#import "HNOrderPaySuccessViewController.h"
#import "HNPaySuccessController.h"

#import "AppDelegate.h"



@interface HNPayTypeController ()<UITableViewDelegate,UITableViewDataSource,HNDocterMsgCellDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *dataSource;

@property (nonatomic,assign)NSInteger currentPayState;

@property (nonatomic,assign)NSString* orderType;

@end

@implementation HNPayTypeController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付方式";
}


-(void)drawView{
    AppDelegate * app = [UIApplication sharedApplication].delegate;
    app.appCustomDelegate = self;
    
    NSArray *imageStr = [NSArray arrayWithObjects:@"payStyle_zhifubao.png",@"payStyle_weixin.png", nil];
    NSArray *titleStr = [NSArray arrayWithObjects:@"支付宝支付",@"微信支付",@"名医咨询", nil];
    NSArray *detailStr = [NSArray arrayWithObjects:@"推进安装支付宝客户端的用户使用",@"推荐安装微信5.0以上版本的用户使用", nil];
    
    NSMutableArray *dataSource = [[NSMutableArray alloc]init];
    for (int i = 0; i<imageStr.count; i++) {
        NSDictionary *cellDic = [NSDictionary dictionaryWithObjectsAndKeys:imageStr[i],@"imageStr",titleStr[i],@"titleStr",detailStr[i],@"detailStr", nil];
        [dataSource addObject:cellDic];
    }
    
    _dataSource = dataSource;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-270)];
    [_tableView setBackgroundColor:[UIColor whiteColor]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    //    _tableView.selectionStyle = UITableViewCellSelectionStyleNone;
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    
    
    //添加点击按钮---添加一个按钮
    UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [clickBtn setTitle:@"支付" forState:UIControlStateNormal];
    [clickBtn setFont:[UIFont systemFontOfSize:15]];
    [clickBtn setBackgroundColor:RGB(252, 103, 105)];
    [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    clickBtn.frame = CGRectMake(20, kScreen_Width - 60, kScreen_Width - 40, 40);
    [clickBtn addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clickBtn];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineLabel];
}





#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataSource count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"HNDocterMsgCell";
    
    HNDocterMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.doctorMsgDelegate = self;
    cell.indexPath = indexPath;
    cell.cellImage.image = [UIImage imageNamed:[_dataSource[indexPath.row] objectForKey:@"imageStr"]];
    cell.cellTitle.text = [_dataSource[indexPath.row] objectForKey:@"titleStr"];
    cell.cellTaskStr.text = [_dataSource[indexPath.row] objectForKey:@"detailStr"];
    [cell.cellTime setHidden:YES];
    
    [cell.cellChoiceImage setHidden:NO];
    
    cell.cellChoiceImage.tag = indexPath.row;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _currentPayState = indexPath.row;
    
}

-(void)HNDocterMsgCell:(HNDocterMsgCell *)doctorMsgCell ChangeChoiceState:(NSIndexPath *)indexPath{

//        doctorMsgCell.imageView.image = [UIImage imageNamed:@"552cc35c851ca_128.png"];

    doctorMsgCell.cellChoiceImage.image = [UIImage imageNamed:@"right_green.png"];
   
}



#pragma mark - 支付
-(void)payAction:(UIButton *)btn{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    NSString *payType = nil;
    if (_currentPayState == 0) {
        payType = @"alipay";
    }else{
        payType = @"weixinPay";
    }
    
    [param setObject:payType forKey:@"payType"];
    
    [param setObject:_orderSn forKey:@"orderSn"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_order_pay parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取支付信息失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            NSDictionary *backGoodsDic = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"backGoodsDic = %@",backGoodsDic);
            

            if (_currentPayState == 0) {
                NSString *sign_content = [NSString stringWithFormat:@"%@",[backGoodsDic objectForKey:@"sign_content"]];
                [HNAllJSExport AliPayOrder:sign_content];
                
//                NSString *orderName = [backGoodsDic objectForKey:@"subject"];
//                NSString *orderpPice = [backGoodsDic objectForKey:@"total_fee"];
//                NSString *orderNumber = [backGoodsDic objectForKey:@"orderSn"];
//                NSString *orderDesc = [backGoodsDic objectForKey:@"body"];
//                
//                
//                [HNAllJSExport AliPayOrderName:orderName andOrderpPice:orderpPice andOrderNumber:orderNumber andOrderDesc:orderDesc];
            }else if(_currentPayState == 1){
                [HNAllJSExport WeiXinPayOrder:backGoodsDic];
//                NSString *orderName = [backGoodsDic objectForKey:@"subject"];
//                NSString *orderpPice = [backGoodsDic objectForKey:@"total_fee"];
//                NSString *orderNumber = [backGoodsDic objectForKey:@"orderSn"];
//                [HNAllJSExport WeiXinPayOrderName:orderName andOrderpPice:orderpPice andOrderNumber:orderNumber];
            }
            
            
            }else{
                NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
                if ([info isEqualToString:msg_InvalidLoginInformation]) {
                    
                    [AppConfig removeUserInfo];
                    
                    [[EMClient sharedClient] logout:YES];
                    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                    
                    LoginViewController *loginVC = [[LoginViewController alloc]init];
                    
                    [self.navigationController pushViewController:loginVC animated:YES];
                }
                [self showHint:info];
    }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取支付信息失败"];
    }];
    
}

#pragma mark - 支付回调
-(void)PingPayCallBack:(BOOL)bol{
    
    if (bol) {

            HNPaySuccessController *paySuccessVC = [[HNPaySuccessController alloc]init];
        paySuccessVC.orderDic = _orderDic;
            [self.navigationController pushViewController:paySuccessVC animated:YES];
//            [self.payTypeDelagete HNPayTypeControllerBackChangeOrderState];
//            [self.navigationController popViewControllerAnimated:YES];
       
        [self showHint:@"支付成功"];
    }else{
        [self showHint:@"支付失败"];
    }
}




@end
